package com.baiqian.doupo.item;

import com.baiqian.doupo.capability.DouPoModCapability;
import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import com.baiqian.doupo.container.NaJieSmallContainProvider;
import com.baiqian.doupo.modgroup.DouPoModGroup;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.text.IFormattableTextComponent;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.NetworkHooks;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class WuPin extends Item {
    public WuPin(Properties properties) {
        super(properties);
    }
}

//金币
class JinBi extends WuPin {
    public JinBi() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(64));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//黑卡
class HeiKa extends WuPin {
    public HeiKa() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(64));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//五纹紫金卡
class WuWenZiJinKa extends WuPin {
    public WuWenZiJinKa() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(64));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//葫芦
class Hulu extends WuPin {
    public Hulu() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(16));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//斗气剑
class DouQiJian extends WuPin {
    public DouQiJian() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(1));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//灵液
class LingYe extends WuPin {
    public LingYe() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//纳戒
class NaJie_Small extends WuPin {
    public NaJie_Small() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(1));
    }
//    @Nullable
//    @Override
//    public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
//        return new NaJieSmallCapabilityProvider(stack, nbt);
//    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (!worldIn.isRemote && handIn == Hand.MAIN_HAND) {
            ItemStack itemStack = playerIn.getHeldItem(handIn);
            NetworkHooks.openGui((ServerPlayerEntity) playerIn, new NaJieSmallContainProvider(itemStack));
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//寄宿药老的戒指
class JiSuYaoLaoDeJieZhi extends WuPin {
    private static int tick = 0;

    public JiSuYaoLaoDeJieZhi() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(1));
    }

    @OnlyIn(Dist.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.RED));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }

    @Override
    public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
        if (!worldIn.isRemote) {
            if (entityIn instanceof PlayerEntity) {
                LazyOptional<IDouQiCapability> douqiCap = entityIn.getCapability(DouPoModCapability.DOUQI_CAPABILTTY);
                if (douqiCap.isPresent()) {
                    douqiCap.ifPresent((l) -> {
                                if (l.getDouQi(2) >= 0.01) {
                                    l.addDouQi(-0.01,2,false,(PlayerEntity) entityIn);
                                    ++tick;
                                    if (tick >= 1) {
                                        l.addDouQiCompress((PlayerEntity) entityIn);//自动修炼
                                        tick = 0;
                                    }
                                }
                            }
                    );
                }
            }
        }
    }
}

//黑铁片
class HeiTiePian extends WuPin {
    public HeiTiePian() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(1));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}

//药渣
class YaoZha extends WuPin {
    public YaoZha() {
        super(new Properties().group(DouPoModGroup.douPoItemGroup).maxStackSize(64));
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(this.getDescription().mergeStyle(TextFormatting.GRAY));
    }

    @OnlyIn(Dist.CLIENT)
    public IFormattableTextComponent getDescription() {
        return new TranslationTextComponent(this.getTranslationKey() + ".1");
    }
}
